#pragma once
#include"data.hpp"

extern cloud::DataManager *_data;
namespace cloud
{
    class HotManeger
    {
        
        bool IsHot(const std::string filename)
        {
            FileUtil fl(filename);
            time_t begin=fl.LastAtime();
            time_t end=time(nullptr);
            if((end-begin)>_hot_time)return false;
            else return true;
        }
        public:
        HotManeger()
        {
            Config *config = Config::GetInstance();
			_back_dir = config->GetBackDir();
			_pack_dir = config->GetPackDir();
			_hot_time = config->GetHotTime();
            FileUtil tem1(_back_dir);
            FileUtil tem2(_pack_dir);
            tem1.CreateDir();
            tem2.CreateDir();
        }
        bool Run()
        {
            while (1)
            {
                FileUtil fl(_back_dir);
            std::vector< std::string> allfile;
            fl.ScanDir(&allfile);
            for(int i=0;i<allfile.size();i++)
            {
                if(IsHot(allfile[i])==false)
                {
                    //不是热点要进行压缩
                    BackupInfo bi;
                    if(_data->GetOneByRealPath(allfile[i],&bi)==false)
                    {
                        BackupInfo bi2(allfile[i]);
                        bi=bi2;
                    }
                    FileUtil temp(allfile[i]);
                    temp.Compress(bi._pack_path);
                    temp.Remove();
                    bi._pack_flag=true;
                    _data->Update(bi);
                }
            }
            usleep(1000);
            
            }
            
            
            return true;
        }
        private:
        int _hot_time;
        std::string _pack_dir;
        std::string _back_dir;


    };

}